package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.OverflowListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wswdkl
 * @version 1.0
 * @date 2022/2/12 11:15
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;
    @Autowired
    private LogService logService;
    @Autowired
    private UserDao userDao;

    /**
     * 添加报溢单
     * @param overflowList
     * @param overflowListGoodsStr
     * @return
     */
    @Override
    public boolean saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {
        //1.先插入商品报损单
        overflowListGoodsDao.saveOverflowList(overflowList);
        Integer overflowListId = overflowList.getOverflowListId();
        //2.再插入商品报损列表
        List<OverflowListGoods> overflowListGoodsList = JSONObject.parseArray(overflowListGoodsStr, OverflowListGoods.class);
        for (OverflowListGoods overflowListGoods : overflowListGoodsList) {
            overflowListGoods.setOverflowListId(overflowListId);
        }
        overflowListGoodsDao.saveOverflowListGoods(overflowListGoodsList);
        logService.save(new Log(Log.UPDATE_ACTION,"保存报溢单"));
        return true;
    }

    /**
     * 查询报溢单
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> queryOverflowList(String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();

        List<OverflowList> overflowLists = overflowListGoodsDao.queryOverflowList(sTime,eTime);
        map.put("rows",overflowLists);

        for (OverflowList overflowList : overflowLists) {
            User user = userDao.getUserById(overflowList.getUserId());
            overflowList.setTrueName(user.getTrueName());
        }

        logService.save(new Log(Log.SELECT_ACTION, "报溢单查询"));
        return map;
    }

    /**
     * 查询报溢单商品信息
     * @param overflowListId
     * @return
     */
    @Override
    public Map<String, Object> queryOverflowGoodsList(Integer overflowListId) {
        Map<String, Object> map = new HashMap<>();

        List<OverflowListGoods> overflowListGoodsList = overflowListGoodsDao.queryOverflowGoodsList(overflowListId);
        map.put("rows",overflowListGoodsList);

        logService.save(new Log(Log.SELECT_ACTION, "报溢单商品信息查询"));
        return map;
    }
}
